package adminDynamicFieldDao

import (
	"gitee.com/krio/ginc/example/core/exerr"
	"gitee.com/krio/ginc/example/core/lib/mysql"
	adminDynamicFieldModel "gitee.com/krio/ginc/example/internal/admin/dynamic/field/model"
	commonEntry "gitee.com/krio/ginc/example/internal/common/entry/common"
	commonModel "gitee.com/krio/ginc/example/internal/common/model/common"
	"github.com/gin-gonic/gin"
	"github.com/xormplus/xorm"
)

type DynamicFieldDao struct {
}

func (d *DynamicFieldDao) DynamicFieldList(ctx *gin.Context, req *commonEntry.ListCommonReq) ([]*adminDynamicFieldModel.DynamicField, int64, error) {
	var (
		total int64
		list  []*adminDynamicFieldModel.DynamicField
		err   error
	)
	// 构建查询条件
	buildQuery := func() *xorm.Session {
		query := mysql.Orm(ctx).Table(adminDynamicFieldModel.DynamicFieldTableName)
		if req.Keyword != "" {
			query.Where("user_name LIKE ?", "%"+req.Keyword+"%")
		}
		if len(req.StatusIn) > 0 {
			query.In("status", req.StatusIn)
		}
		for _, v := range req.OrderBy {
			if v.IsDesc {
				query.Desc(v.Field)
			} else {
				query.Asc(v.Field)
			}
		}
		return query
	}
	// 克隆查询对象用于不同用途
	countQuery := buildQuery()
	total, err = countQuery.Count(new(adminDynamicFieldModel.DynamicField))
	if err != nil {
		return nil, 0, err
	}
	if total < 0 {
		return nil, 0, nil
	}
	listQuery := buildQuery()
	if req.IsAll {
		if total > 10000 {
			total = 10000
		}
		listQuery.Limit(int(total))
	} else {
		offset := (req.Page - 1) * req.Limit
		listQuery.Limit(int(req.Limit), int(offset))
	}
	err = listQuery.Find(&list)
	if err != nil {
		return nil, 0, err
	}
	return list, total, nil
}

func (d *DynamicFieldDao) DynamicFieldDetail(ctx *gin.Context, id int64) (*adminDynamicFieldModel.DynamicField, bool, error) {
	one := &adminDynamicFieldModel.DynamicField{}
	has, err := mysql.Orm(ctx).Table(adminDynamicFieldModel.DynamicFieldTableName).
		ID(id).Get(one)
	if err != nil {
		return nil, false, err
	}
	return one, has, nil
}

func (d *DynamicFieldDao) DynamicFieldDetailByInfo(ctx *gin.Context, info *adminDynamicFieldModel.DynamicField) (*adminDynamicFieldModel.DynamicField, bool, error) {
	query := mysql.Orm(ctx).Table(adminDynamicFieldModel.DynamicFieldTableName)
	if info.ID > 0 {
		query.ID(info.ID)
	}
	if info.Key != "" {
		query.Where("key = ?", info.Key)
	}
	if info.Status != commonModel.DataUnknown {
		query.Where("status = ?", info.Status)
	}
	one := &adminDynamicFieldModel.DynamicField{}
	has, err := query.Get(one)
	if err != nil {
		return nil, false, err
	}

	return one, has, nil
}

func (d *DynamicFieldDao) DynamicFieldUpdate(ctx *gin.Context, one *adminDynamicFieldModel.DynamicField) (int64, error) {
	if one.ID <= 0 {
		return 0, exerr.ParamInvalid
	}
	_, err := mysql.Orm(ctx).Table(adminDynamicFieldModel.DynamicFieldTableName).
		ID(one.ID).Update(one)
	if err != nil {
		return 0, err
	}
	return one.ID, nil
}

func (d *DynamicFieldDao) DynamicFieldAdd(ctx *gin.Context, one *adminDynamicFieldModel.DynamicField) (int64, error) {
	_, err := mysql.Orm(ctx).Table(adminDynamicFieldModel.DynamicFieldTableName).Insert(one)
	if err != nil {
		return 0, err
	}
	return one.ID, nil
}

func (d *DynamicFieldDao) DynamicFieldDelete(ctx *gin.Context, ids []int64) error {
	_, err := mysql.Orm(ctx).Table(adminDynamicFieldModel.DynamicFieldTableName).
		Cols("status").
		In("id", ids).Update(&adminDynamicFieldModel.DynamicField{Status: commonModel.DataDelete})
	if err != nil {
		return err
	}
	return nil
}
